package chpt02;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class PersonInfoManagement {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<String> inputList = new ArrayList<>();
        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();

        // 输入个人信息
        int n = scanner.nextInt();
        scanner.nextLine(); // 读取换行符
        for (int i = 0; i < n; i++) {
            inputList.add(scanner.nextLine());
        }

        // 输入x和y
        String x = scanner.next();
        String y = scanner.next();

        // 将所有姓为x的字符串放入list1中
        for (String info : inputList) {
            String[] parts = info.split("-");
            if (parts[0].equals(x)) {
                list1.add(info);
            }
        }
        System.out.println("list1: " + list1);

        // 将list1中的名字为y的字符串取出放入list2中
        for (String info : list1) {
            String[] parts = info.split("-");
            if (parts[1].equalsIgnoreCase(y)) {
                list2.add(info);
            }
        }
        System.out.println("list2: " + list2);

        // 对list2中的成绩求取平均值输出
        double sum = 0;
        for (String info : list2) {
            String[] parts = info.split("-");
            sum += Double.parseDouble(parts[2]);
        }
        double average = sum / list2.size();
        System.out.println("Average score in list2: " + average);

        // 使用冒泡排序对list2中数据按照成绩降序排序输出
        for (int i = 0; i < list2.size() - 1; i++) {
            for (int j = 0; j < list2.size() - i - 1; j++) {
                String[] parts1 = list2.get(j).split("-");
                String[] parts2 = list2.get(j + 1).split("-");
                if (Double.parseDouble(parts1[2]) < Double.parseDouble(parts2[2])) {
                    String temp = list2.get(j);
                    list2.set(j, list2.get(j + 1));
                    list2.set(j + 1, temp);
                }
            }
        }
        System.out.println("Sorted list2: " + list2);
    }
}